package com.zllkj.mm.api.network.subscriber;


import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.zllkj.mm.MyApp;
import com.zllkj.mm.api.network.exception.ApiException;
import com.zllkj.mm.api.view.ProgressCancelListener;
import com.zllkj.mm.api.view.SimpleLoadDialogHandler;
import com.zllkj.mm.common.L;
import com.zllkj.mm.util.common.NetworkUtils;

import java.io.IOException;

import retrofit2.adapter.rxjava.HttpException;

public abstract class ProgressSubscriber<T> extends BaseSubscriber<T> implements ProgressCancelListener {


    protected abstract void onSucc(T t);


    protected void onFail(int code, String msg) {
        if (msg.contains("token")) {
            L.showToast("登录信息已失效");
            MyApp.getContext().sendBroadcast(new Intent("token_fail"));
            return;
        }
        L.showToast(msg);
    }


    private SimpleLoadDialogHandler dialogHandler;


    public ProgressSubscriber(Context context) {
        dialogHandler = new SimpleLoadDialogHandler(context, this, true);
    }


    /**
     * 显示Dialog
     */
    public void showProgressDialog() {
        if (dialogHandler != null) {
//            dialogHandler.obtainMessage(SimpleLoadDialog.SHOW_PROGRESS_DIALOG).sendToTarget();
            dialogHandler.show();
        }
    }


    /**
     * 隐藏Dialog
     */
    private void dismissProgressDialog() {
        if (dialogHandler != null) {
//            dialogHandler.obtainMessage(SimpleLoadDialog.DISMISS_PROGRESS_DIALOG).sendToTarget();
            dialogHandler.dismiss();
            dialogHandler = null;
        }
    }


    @Override
    public void onStart() {
        super.onStart();
        showProgressDialog();
    }

    @Override
    public void onCompleted() {
        dismissProgressDialog();
    }


    @Override
    public void onError(Throwable e) {
        e.printStackTrace();

        Log.d("acthome","error---->" + e.getMessage());
        String errorMsg = "";
        if (e.getMessage() != null) {
            errorMsg ="数据异常";
        }
        if (e instanceof IOException) {
            if (NetworkUtils.isAvailable(MyApp.getInstance())) {
                errorMsg = "网络不可用" + e.getMessage();
            } else {
                errorMsg = "网络出错了" + e.getMessage();
            }
            onFail(-1, errorMsg);
        } else if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
//            errorMsg = httpException.response().message();
            errorMsg = "网络连接异常";
            onFail(-1, errorMsg);
        } else if (e instanceof ApiException) {
            ApiException exception = (ApiException) e;
            int code = exception.getErrorCode();
            onFail(code, e.getMessage());
        } else if (e instanceof ClassCastException) {
            errorMsg = "数据类型转换错误";
            onFail(-1, errorMsg);
        } else {
            onFail(-1, errorMsg);
        }
        dismissProgressDialog();
    }

    @Override
    public void onNext(T t) {
        super.onNext(t);
        onSucc(t);
    }

    @Override
    public void onCancelProgress() {
        if (!this.isUnsubscribed()) {
            this.unsubscribe();
        }
    }

}
